package com.sensor.adapter;


import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.landmesure.bluetoothchat.R;
import com.sensor.core.DeviceStatus;

import java.util.List;

public class ListViewAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<DeviceStatus> mDatas;

    public ListViewAdapter(Context context, List<DeviceStatus> datas) {

        mInflater = LayoutInflater.from(context);
        mDatas = datas;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.layout_item_listview, parent, false);
            holder = new ViewHolder();
            holder.name = convertView.findViewById(R.id.name);
            holder.status = convertView.findViewById(R.id.status);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        DeviceStatus deviceStatus = mDatas.get(position);
        if (deviceStatus.getStatus()==0){
            Drawable drawable = convertView.getResources().getDrawable(R.drawable.normal);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            holder.status.setCompoundDrawables(drawable,null,null,null);
        }
        else if (deviceStatus.getStatus()==1){
            Drawable drawable = convertView.getResources().getDrawable(R.drawable.run);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            holder.status.setCompoundDrawables(drawable,null,null,null);
        }
        else {
            Drawable drawable = convertView.getResources().getDrawable(R.drawable.error);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            holder.status.setCompoundDrawables(drawable,null,null,null);
        }

        holder.name.setText(deviceStatus.getName());
        holder.status.setText(String.valueOf(deviceStatus.getStatus()));


        return convertView;
    }

    private class ViewHolder {
        TextView id;
        TextView name;
        TextView status;
    }

}
